

local j = 1
foreach month in Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec {
if "`mymonth'"=="`month'" {
if `j'<10 {
local mymonth = "0"+"`j'"
}
else {
local mymonth = `j'
}
}
local j = `j'+1
}




local j = 1
local year 2020
local month 02

forvalues day = 24/29 {

import delimited "https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-province/dpc-covid19-ita-province-`year'`month'`day'.csv", clear stringcols(11)
export excel using "../2_Data/raw/province_daily/province_`year'`month'`day'.xls", replace firstrow(variables)
tempfile `j'
save ``j'', replace
local j = `j'+1
}

foreach month in "03" "05" "07" "08" "10" "12" {

forvalues day = 1/31 {
if `day'<10 {
local day = "0"+"`day'"
}
import delimited "https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-province/dpc-covid19-ita-province-`year'`month'`day'.csv", clear stringcols(11)
export excel using "../2_Data/raw/province_daily/province_`year'`month'`day'.xls", replace firstrow(variables)
tempfile `j'
save ``j'', replace
local j = `j'+1
}
}

foreach month in "04" "06" "09" "11" {

forvalues day = 1/30 {
if `day'<10 {
local day = "0"+"`day'"
}
import delimited "https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-province/dpc-covid19-ita-province-`year'`month'`day'.csv", clear stringcols(11)
export excel using "../2_Data/raw/province_daily/province_`year'`month'`day'.xls", replace firstrow(variables)
tempfile `j'
save ``j'', replace
local j = `j'+1
}
}

local year = 2021

local month "01"

forvalues day = 1/31 {
if `day'<10 {
local day = "0"+"`day'"
}
import delimited "https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-province/dpc-covid19-ita-province-`year'`month'`day'.csv", clear stringcols(11)
export excel using "../2_Data/raw/province_daily/province_`year'`month'`day'.xls", replace firstrow(variables)
tempfile `j'
save ``j'', replace
local j = `j'+1
}


local J = `j'-1

use `1', clear

forvalues j = 2/`J' {
append using ``j''
}


replace data = substr(data,1,10)
gen day = substr(data,9,2)
gen month = substr(data,6,2)
gen year = substr(data,1,4)

drop data 
gen data = day+"/"+month+"/"+year
gen date = date(data,"DMY")

drop data day month year

format date %td

drop stato note
rename v9 longit

order date codice_regione denominazione_regione codice_provincia denominazione_provincia

sort denominazione_provincia date

rename (denominazione_regione denominazione_provincia) (region province)

replace region = strupper(region)
replace province = strupper(province)

replace province = "FORLI'-CESENA" if strpos(province,"CESENA")!=0
drop if province=="FUORI REGIONE / PROVINCIA AUTONOMA"
drop if province=="IN FASE DI DEFINIZIONE/AGGIORNAMENTO"

replace codice_regione=21 if strpos(region,"BOLZANO")!=0
replace codice_regione=22 if strpos(region,"TRENTO")!=0




save ../2_Data/temp/data_covid_province.dta, replace

collapse (first) region, by(province)

save "../2_Data/temp/data_skeleton.dta", replace




local j = 1
local year 2020
local month 02

forvalues day = 24/29 {

import delimited "https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-regioni/dpc-covid19-ita-regioni-`year'`month'`day'.csv", clear stringcols(21)
export excel using "../2_Data/raw/region_daily/region_`year'`month'`day'.xls", replace firstrow(variables)

cap drop note*
tempfile `j'
save ``j'', replace
local j = `j'+1
}

foreach month in "03" "05" "07" "08" "10" "12" {

forvalues day = 1/31 {
if `day'<10 {
local day = "0"+"`day'"
}
import delimited "https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-regioni/dpc-covid19-ita-regioni-`year'`month'`day'.csv", clear stringcols(21)
export excel using "../2_Data/raw/region_daily/region_`year'`month'`day'.xls", replace firstrow(variables)

cap drop note*
tempfile `j'
save ``j'', replace
local j = `j'+1
}
}

foreach month in "04" "06" "09" "11" {

forvalues day = 1/30 {
if `day'<10 {
local day = "0"+"`day'"
}
import delimited "https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-regioni/dpc-covid19-ita-regioni-`year'`month'`day'.csv", clear stringcols(21)
export excel using "../2_Data/raw/region_daily/region_`year'`month'`day'.xls", replace firstrow(variables)

cap drop note*
tempfile `j'
save ``j'', replace
local j = `j'+1
}
}

local month "01"
local year 2021

forvalues day = 1/31 {
if `day'<10 {
local day = "0"+"`day'"
}
import delimited "https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-regioni/dpc-covid19-ita-regioni-`year'`month'`day'.csv", clear stringcols(21)
export excel using "../2_Data/raw/region_daily/region_`year'`month'`day'.xls", replace firstrow(variables)

cap drop note*
tempfile `j'
save ``j'', replace
local j = `j'+1
}

local J = `j'-1

use `1', clear

forvalues j = 2/`J' {
append using ``j''
}


replace data = substr(data,1,10)
gen day = substr(data,9,2)
gen month = substr(data,6,2)
gen year = substr(data,1,4)

drop data 
gen data = day+"/"+month+"/"+year
gen date = date(data,"DMY")

drop data day month year

format date %td

drop stato
rename v6 longit

order date codice_regione denominazione_regione

sort denominazione_regione 

rename (denominazione_regione) (region)

replace region = strupper(region)

sort region date
by region: gen tamponi_day = tamponi-tamponi[_n-1]

drop if region==""

replace codice_regione=21 if strpos(region,"BOLZANO")!=0
replace codice_regione=22 if strpos(region,"TRENTO")!=0


save "../2_Data/temp/data_covid_region.dta", replace

collapse (first) codice_regione, by(region)

save "../2_Data/temp/data_skeleton_region.dta", replace
